import {
	beforeEach,
	afterNotNext
} from "uni-crazy-router";
import store from '@/store';

let intercept;
export function bindInterceptLogin() {
	// 销毁登陆拦截
	destroyInterceptLogin();
	// 设置登陆拦截
	intercept = beforeEach(async (to, from, next) => {
		// console.log('interceptLogin.beforeEach', {to, from, next});
		//登陆拦截
		if (to.url !== 'pages/sys/login/agreement' && to.url !== 'pages/sys/login/forgetPassword' &&
			to.url !== 'pages/sys/login/login' && to.url !== 'pages/sys/login/register' &&
			to.url !== 'pages/sys/login/setter') {
			if (!store.getters.hasLogin) {
				// 拦截路由，并且跳转去登录页
				uni.navigateTo({
					url: '/pages/sys/login/login',
					passedParams: {
						info: '未登陆',
					},
				});
				return;
			}
		}
		// console.log(to.url);
		next();
	})
}
export function destroyInterceptLogin() {
	// console.log('interceptLogin.destroyInterceptLogin', '销毁登陆拦截');
	if (intercept) {
		intercept() // 销毁拦截
		intercept = null
	}
}
